package com.generator.sysManage.moduleManage.controller;


import com.generator.sysManage.moduleManage.pojo.Module;
import com.generator.sysManage.moduleManage.service.impl.ModuleManageService;
import com.generator.sysManage.positionManage.pojo.JMJob;
import com.generator.sysManage.positionManage.service.impl.JMService;
import com.generator.sysManage.sectionManage.pojo.DMDept;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * ModuleManageService
 * @author WJW
 * @since ....
 * @version ......
 */
@Controller
@RequestMapping("/moduleManage")
public class ModuleManageController {

    @Autowired
    private ModuleManageService mService;

    public ModuleManageService getmService() {
        return mService;
    }
    public void setmService(ModuleManageService mService) {
        this.mService = mService;
    }

    //模块列表（分页）
    //搜索
    @RequestMapping("/moduleList")
    public String moduleList(Model model, Module module, PageModel<Module> pageModel){
        PageModel<Module> moduleList = mService.getModuleListByFY(module,pageModel);
        model.addAttribute("moduleList",moduleList);
        return "sys/modules/moduleList";
    }


    //根据条件查询所有父模块
    @ResponseBody
    @RequestMapping("/moduleList1")
    public List<Module> moduleList(Module module){
        return mService.getModuleListByJMJob(module);
    }


    //搜索
    @RequestMapping("/queryModule")
    public String queryModule(Model model, Module jmJob){
        String trim = jmJob.getModule_rname().trim();
        jmJob.setModule_rname(trim);
        List<Module> userList = mService.getModuleListByJMJob(jmJob);
        model.addAttribute("moduleList",userList.get(0));
        return "sys/modules/moduleView";
    }


    //模块名称局部验证
    @RequestMapping("/module_rnameYZ")
    @ResponseBody
    public String module_rnameYZ(Module jmJob){
        List<Module> dmDeptList = mService.getModuleListByJMJob(jmJob);
        if(dmDeptList!=null&&dmDeptList.size()==1){
            return "true";
        }
        return "false";
    }


    //进入添加模块页面
    @RequestMapping("/moduleAdd")
    public String moduleAdd(){
        return "sys/modules/moduleAdd";
    }


    //验证该父模块下的此模块是否存在
    @RequestMapping("/moduleYZ")
    @ResponseBody
    public String moduleYZ(Module module){
        List<Module> moduleList= mService.getModuleListByJMJob(module);
        if(moduleList!=null && moduleList.size()>=1){
            return "true";
        }
        else {
            return "false";
        }
    }


    //添加模块
    @RequestMapping("/moduleAdd1")
    @ResponseBody
    public String moduleAdd1(Module jmJob){
        jmJob.setModule_state("0");
        Integer add = mService.addModule(jmJob);
        if(add==1){
            return "true";
        }
        return "false";
    }



    //进入修改模块页面
    @RequestMapping("/moduleUpdate")
    public String updateView(Module module,Model model,HttpSession session){
        return mService.updateView(module,model,session);
    }


    //修改模块
    @RequestMapping("/updateModule")
    @ResponseBody
    public String updatePosition(Module jmJob,HttpSession session){
        String job_id = (String) session.getAttribute("module_id");
        jmJob.setModule_id(job_id);
        Integer updateJMJob = mService.updateModule(jmJob);
        if(updateJMJob==1){
            return "true";
        }
        return "false";
    }


    //修改部门状态
    @RequestMapping("/updateState")
    public String updateStatu(Module jmJob,Model model){
        return mService.updateState(jmJob,model);
    }



}
